package vivace.view;

import vivace.model.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.util.*;

import lib.Komponenter.CTable;

/***
 * An internal frame that contains a project. 
 */
public class ProjectFrame extends JInternalFrame implements Observer {

    private Integer key;
    private Project model;
    private Controller controller;

    /**
     * @return The key of the project
     */
	public Integer getKey(){
    	return this.key;
    }

	/**
	 * Constructor
	 * @param key The key of the project
	 * @param model The model to use
	 */
    public ProjectFrame( Integer key, Project model ) {
   
    	// Call the super class
    	super(model.getName(), true, true, true, true);

    	// Assign the model and the controller 
    	this.model = model;
    	this.model.addObserver(this);
    	this.controller = new Controller(this);
    	
    	// Set some GUI parameters
    	ImageIcon i = new ImageIcon("resources/iconSongFrame.gif");
    	this.frameIcon = i;
        this.setTitle(this.model.getName());
        this.key = key;
        
        /* Create tracks frame */
        
        TracksFrame tracks = new TracksFrame(model.getSequencer());
       
        
        this.add(tracks);
        tracks.setVisible(true);
        this.pack();
        
    }
    
    @Override
    public void update(Observable o, Object arg){
    	
    	// No matter which action was performed, the model has changed.
    	// Display this by adding a (*) to the frame's title
    	if( !this.getTitle().contains(" (*)"))
    		this.setTitle( this.getTitle() + " (*)");
    	
    	// Check which type of action that was performed
    	Project.Action action = this.model.getAction();
    	
    	// Then perform the desired updates depending on the action
    	switch( action ){
    		
    		case SAVED:
    			
    			// Remove the (*) from the frame title
    			this.setTitle(this.getTitle().replace("(*)",""));
    			break;
    		
    	}
    }

    // Controller
    private class Controller implements ActionListener{
    	
    	private ProjectFrame view;
    	private Project model;
    	
    	/**
    	 * Constructor
    	 * @param view The view that is controlled
    	 */
    	public Controller( ProjectFrame view ){
    		this.view = view;
    		this.model = view.model;
    	}
    	
        @Override
		public void actionPerformed(ActionEvent e) {
	    	
	    	// Extract the command string
	    	String cmd = e.getActionCommand();

	    	// Do something :)
	    	
	    	
	    	// TODO: Ta bort sen
	    	//this.model.dummy();
			
		}
    }

}


